package com.medical.controller;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.config.AlipayConfig;
import com.medical.HttpTools.DataAnalyze;
import com.medical.HttpTools.HttpRequest;
import com.medical.po.City;
import com.medical.po.Doctorinfo;
import com.medical.po.DoctorinfoCustom;
import com.medical.po.DoctorlogininfoCustom;
import com.medical.po.DoctortitleCustom;
import com.medical.po.Hospitaldept;
import com.medical.po.HosplevelCustom;
import com.medical.po.HosporderCustom;
import com.medical.service.DoctorinfoService;
import com.medical.service.HospinfoService;

@Controller
@RequestMapping(value="Main/doctor")
/**
 * 
 * @author LTT-AH-ZY
 * @date 创建时间：2017年7月24日 下午5:04:01 
 * @version 1.0
 * @parameter 参数及其意义
 * @return 返回值
 * @throws 异常类及抛出条件
 */
public class DoctorController {
	/**
	 * 根据当前日期生成其所在的一个星期，返回一个datelist
	 * @param mdate
	 * @return
	 */
	/*public static List<Date> dateToWeek(Date mdate) {
		int b = mdate.getDay();//获取当前日期是一个星期的第几天
		Date fdate;
		List<Date> list = new ArrayList<Date>();
		Long fTime = mdate.getTime() - b * 24 * 3600000;
		for (int a = 1; a <= 7; a++) {
			fdate = new Date();
			fdate.setTime(fTime + (a * 24 * 3600000));
			list.add(a-1, fdate);
		}
		return list;
	}*/
	/**
	 * 工具类，用以将int数字返回对应的星期
	 * @param index
	 * @return
	 */
	/*public String getWeek(int index) {
		String[] WeekStr={"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
		return WeekStr[index];
	}*/
	/**
	 * 返回星期的工具类，返回当前日期代表的一个星期的日期
	 * @return
	 */
	/*public List<String> getTime(){
//		int currentDay;
		List<String> week = new ArrayList<>();
        Date time= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEE"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        List<Date> days = dateToWeek(time);
		System.out.println("今天的日期: " + sdf.format(time));
		for (Date date : days) {
			System.out.println(sdf.format(date));
		}
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK)-1;// 获得当前日期是一个星期的第几天
//        currentDay=day;
//        if(currentDay==0) {
//        	currentDay=7;
//        }
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        for(int i=0;i<7;i++) {
            week.add(sdf.format(cal.getTime())+"/"+getWeek(i));
            cal.add(Calendar.DATE,1);
            //currentDay++;
//            if(currentDay>7) {
//            	currentDay=currentDay%7;
//            }
        }
        return week;
    }*/
	/**
	 * 新的工具类，实现将生成的星期list加入到week中
	 * @return
	 */
	/*public List<String> getNewTime(){
		List<String> week = new ArrayList<>();
        Date time= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd EEE"); // 设置时间格式
        List<Date> days = dateToWeek(time);
		//System.out.println("今天的日期: " + sdf.format(time));
		for (Date date : days) {
			//System.out.println(sdf.format(date));
			week.add(sdf.format(date));
		}
		return week;
	}*/
	/**
	 * 注入DoctorinfoService服务
	 */
	@Autowired
	private DoctorinfoService doctorinfoService ;
	@Autowired
	private HospinfoService hospinfoService;
	/**
	 * 医生列表的初始化，查询所有医生的信息
	 * @param check
	 * @param doctorinfoCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/alldoctorshow")
	public ModelAndView AllDoctorShow(String hosplevel ,String doctortitle ,String details,
			Integer page,DoctorinfoCustom doctorinfoCustom,
			Integer province_code,Integer city_code,Integer area_code,
			Integer One_level_dept,Integer Two_level_dept,
			DoctorlogininfoCustom doctorlogininfoCustom,ModelAndView modelandview,HttpServletRequest request) throws Exception{
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		if(page==null) {
			page=1;
		}
		
		//List<String> list = getNewTime();
		Date time= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd EEE"); // 设置时间格式
        System.out.println("输出接收的数据："+hosplevel+"/"+doctortitle+"/"+details);
        String param = "page="+page+"&hosploginid="+hosploginid+"&hosplevel="+hosplevel+"&doctortitle="+doctortitle+"&details="+details;
        String param1 = "page="+page+"&hosploginid="+hosploginid+"&hosplevel="+hosplevel.trim()+"&doctortitle="+doctortitle.trim()+"&details="+details+
        		"&province_code="+change(province_code)+"&city_code="+change(city_code)+"&area_code="+change(area_code)+
        		"&One_level_dept="+change(One_level_dept)+"&Two_level_dept="+change(Two_level_dept);
        String Backparam = "hosploginid="+hosploginid+"&hosplevel="+hosplevel.trim()+"&doctortitle="+doctortitle.trim()+"&details="+details+
        		"&province_code="+change(province_code)+"&city_code="+change(city_code)+"&area_code="+change(area_code)+
        		"&One_level_dept="+change(One_level_dept)+"&Two_level_dept="+change(Two_level_dept);
        modelandview.addObject("Backparam",Backparam);
        System.out.println("输出param1="+param1);
        List<Map<String, String>> doctorlist = new ArrayList<>();
        if(hosploginid!=null) {
        	try {
        		JSONObject jsondata = HttpRequest.sendPost("listdoctor",param1);
                System.out.println("输出返回的json信息："+jsondata);
                JSONObject json = jsondata.getJSONObject("data");
                JSONArray jsonarray = json.getJSONArray("BaseData");
                Integer jsonarraypage = Integer.parseInt(json.get("page").toString());
                Integer jsonarrayallcount = Integer.parseInt(json.get("allcount").toString());
                System.out.println("输出当前页和总的页数："+jsonarraypage+"----"+jsonarrayallcount);
                System.out.println("jsonarray:"+jsonarray);
                modelandview.addObject("page",jsonarraypage);
                doctorlist = DataAnalyze.DoctorJsonToList(jsonarray);
                modelandview.addObject("allcount", jsonarrayallcount);//返回总页数
                modelandview.addObject("doctorlist", doctorlist);
                
			} catch (Exception e) {
				System.out.println("出错了，输出错误信息是："+e.getMessage());
				modelandview.addObject("page",0);
	            modelandview.addObject("allcount", 0);//返回总页数
	            modelandview.addObject("doctorlist", null);
			}
        }
        //System.out.println("........"+doctorlist);
        
        /*if(page==null) {
        	doctorlogininfoCustom.setIndex(1);//初始化前六个医生信息，limit 0,6
        	modelandview.addObject("page",1);
        }else {
        	Integer pagenum = page;
            int index = (pagenum-1)*6;
            doctorlogininfoCustom.setIndex(index);
            modelandview.addObject("page",pagenum);
        }*/
        //List<DoctorlogininfoCustom> pageDoctorList = doctorinfoService.paginationDoctorList(doctorlogininfoCustom);
        //System.out.println("运行这里。。。");
		//List<DoctorlogininfoCustom> doctorlogininfo = doctorinfoService.findDoctorloginList(doctorlogininfoCustom);
		//int count=doctorinfoService.countdoctorList();
		//modelandview.addObject("doctorlogininfo", pageDoctorList);
		//int size=(int) ((count%6==0)?count/6:count/6+1);//对医生信息表进行count，计算每页6个共多少页。
		//modelandview.addObject("list", list);//返回当前日期代表的一个星期list
		//modelandview.addObject("today", sdf.format(time));//返回当前日期，用以判断日志表当前日期背景高亮
		modelandview.setViewName("MainFrame/Doctor/AllDoctorShow");
		return modelandview;
	}
	public Integer change(Integer data) {
		if(data==-1) {
			return 0;
		}else {
			return data;
		}
	}
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getcalendar")
	public @ResponseBody Map<String,Object> getcalendar(HttpServletRequest request)throws Exception{
		String docloginid=request.getParameter("docloginid");
		System.out.println("输出这个docloginid="+docloginid);
		String param="docloginid="+docloginid;
		Map<String,Object> listmap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject listjson = HttpRequest.sendPost("getcalendar", param);
			String jsoncode = listjson.get("code").toString();
			String jsonmsg = listjson.get("msg").toString();
			
			listmap.put("code",jsoncode);
			listmap.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					if(listjson.get("data").toString()!="null") {
						JSONArray data = listjson.getJSONArray("data");
						list=DataAnalyze.CalendarJsonToList(data);
						listmap.put("list", list);
					}
				} catch (Exception e) {
					System.out.println("解析出错了，出错信息是："+e.getMessage());
					listmap.put("list", "");
				}
			}
		} catch (Exception e) {
			System.out.println("数据传输出错了，错误信息是："+e.getMessage());
			listmap.put("list", "");
		}
		return listmap;
	}
	
	/**
	 * 分页代码
	 * @param page
	 * @param doctorlogininfoCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doctorpagination")
	public @ResponseBody List<DoctorlogininfoCustom> doctorpagination(String page,DoctorlogininfoCustom doctorlogininfoCustom)throws Exception{
		Integer pagenum = Integer.parseInt(page);
        int index = (pagenum-1)*6;
        doctorlogininfoCustom.setIndex(index);
        List<DoctorlogininfoCustom> pageDoctorList = doctorinfoService.paginationDoctorList(doctorlogininfoCustom);
        System.out.println("运行这里。。。");
		return pageDoctorList;
	}
	/**
	 * 初始化界面，但不返回任何数据
	 * @return
	 */
	@RequestMapping(value="/alldoctorshow1")
	public String AllDcotorShow1(){
		return "MainFrame/Doctor/AllDoctorShow";
	}
	/**
	 * 测试。。。
	 * @return
	 * @throws Exception
	 */
	/*@RequestMapping(value="/calendarTest")
	public ModelAndView calendarTest()throws Exception{
		List<String> list = getTime();
		System.out.println("测试："+list);
		ModelAndView model = new ModelAndView();
		model.addObject("list", list);
		model.setViewName("MainFrame/Doctor/Calendar");
		return model;
	}*/
	/**
	 * 我的订单初始化
	 * @param hosporderCustom
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/myorder")
	public ModelAndView myorder() throws Exception{
		
		//hosporderCustom.setHospid(hospid);
		//List<HosporderCustom> hosporderList = doctorinfoService.findHospOrderList(hosporderCustom);
		//System.out.println("输出："+hosporderList);
		ModelAndView model = new ModelAndView();
		//model.addObject("hosporderList", hosporderList);
		model.setViewName("MainFrame/Doctor/MyOrderList");
		return model;
	}
	/**
	 * 异步动态请求列表数据，我的会诊记录
	 * @param limit
	 * @param offset
	 * @param type
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/MyOrderListAjax")
	public @ResponseBody Map<String,Object> MyOrderListAjax(Integer limit,Integer offset,String type,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		String MyOrderParam = "limit="+limit+"&offset="+offset+"&type="+type+"&hosploginid="+hosploginid;//参数串
		String jsonobjectmsg = "";
		String jsonobjectcode ="";
		Integer total=0;
		List<Map<String,Object>> myorderlist=new ArrayList<>();
		if(hosploginid!=null) {
			try {
				JSONObject OrderListjson = HttpRequest.sendPost("listconsultation",MyOrderParam);
				System.out.println("输出接收到的json数据是："+OrderListjson);
				jsonobjectmsg = OrderListjson.get("msg").toString();
				jsonobjectcode =OrderListjson.get("code").toString();
				try {
					if(OrderListjson.get("data").toString()!="null") {
						//System.out.println("data:"+OrderListjson.get("data"));
						JSONObject jsonobjectdata =OrderListjson.getJSONObject("data");
						JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
						total = Integer.parseInt(jsonobjectdata.get("total").toString());
						myorderlist = DataAnalyze.MyOrderJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息1是："+e.getMessage());
					jsonobjectmsg = "数据解析错误";
					jsonobjectcode ="-300";
				}
			} catch (Exception e) {
				System.out.println("出错信息2是："+e.getMessage());
				jsonobjectmsg = "数据传输错误";
				jsonobjectcode ="-200";
			}
		}else {
			jsonobjectmsg = "请先登录";
			jsonobjectcode ="-100";
		}
		Map<String, Object> map = new HashMap<String, Object>();  
		map.put("msg", jsonobjectmsg);
		map.put("rows", myorderlist);
		map.put("code", jsonobjectcode);
        map.put("total", total);  
        
        System.out.println("输出这个map是："+map);
		return map;
	}
	/**
	 * 会诊订单结算，医院和医生之间的订单，非总订单！
	 * @param req
	 * @param rep
	 * @param hosporderid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/OrderSettle")
	public void OrderSettle(HttpServletRequest req,HttpServletResponse rep,String hosporderid)throws Exception{
		HttpSession session=req.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String Param="hosploginid="+hosploginid+"&hosporderid="+hosporderid;
		System.out.println("输出param是："+Param);
		ModelAndView model = new ModelAndView();
		/*//获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = "20171234567";
        //付款金额，必填
        String total_amount = "200";
        //订单名称，必填
        String subject = "测试订单";
        //商品描述，可空
        String body = "商品描述";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
                + "\"total_amount\":\""+ total_amount +"\"," 
                + "\"subject\":\""+ subject +"\"," 
                + "\"body\":\""+ body +"\"," 
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result1 = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("输出result1："+result1);*/
        JSONObject json = HttpRequest.sendPost("paydoctor",Param);
		System.out.println("医院确认押金增加返回json："+json);
        Integer code = Integer.parseInt(json.get("code").toString());
        String msg = json.get("msg").toString();
        rep.setContentType("text/html;charset=" + AlipayConfig.charset);
        if(code==200) {
        	PrintWriter out = rep.getWriter();
        	out.print("<h4>");
        	out.println(msg);
            out.print("</h4>");
        	out.println("<script language='javascript'>");   
            out.println("alert('"+msg+"');");
            out.print("</script>");
            model.setViewName("MainFrame/ErrorsView/PayError");
            model.addObject("code", code);
            model.addObject("msg", msg);
        }else {
        	String result = json.get("data").toString();
        	//rep.getWriter().print("<a>"+msg+"</a>");//直接将完整的表单html输出到页面
        	rep.getWriter().write(result);//直接将完整的表单html输出到页面
            rep.getWriter().flush();
            rep.getWriter().close();
        }
	}
	/**
	 * 支付成功页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/PaySuccess")
	public ModelAndView PaySuccess()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/ErrorsView/PaySuccess");
		return model;
	}
	/**
	 * 结束订单，会诊完成！
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/finishpreorder")
	public @ResponseBody Map<String,Object> finishpreorder(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		Integer hosporderid = Integer.parseInt(request.getParameter("hosporderid"));
		String param = "hosploginid="+hosploginid+"&hosporderid="+hosporderid;
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			JSONObject json = HttpRequest.sendPost("/finishpreorderrequest", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg =  json.get("msg").toString();
			
			map.put("code", jsoncode);
			map.put("msg", jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			map.put("code", -100);
			map.put("msg", "数据传输错误！");
		}
		return map;
	}
	
	/**
	 * 发送网络请求，异框架发送数据以及接收数据处理后的结果
	 * @return
	 */
	@RequestMapping(value="/sendRequest")
	public @ResponseBody Map<String, Object> sendRequest(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		String docloginid = request.getParameter("docloginid");
		String orderprice = request.getParameter("orderprice");
		String ChooseDocTime = request.getParameter("ChooseDocTime");
		String traffictype = request.getParameter("traffictype");
		String trafficprice = request.getParameter("trafficprice");
		String accommondationtype = request.getParameter("accommondationtype");
		String accommondationprice = request.getParameter("accommondationprice");
		String diettype = request.getParameter("diettype");
		String dietprice = request.getParameter("dietprice");
		String orderabs = request.getParameter("orderabs");
		System.out.println("输出接收的数据是：docloginid:"+docloginid+"/hosploginid:"+hosploginid+"/orderprice:"+orderprice);
		//发送 GET 请求
		String Param = "hosploginid="+hosploginid+"&docloginid="+docloginid+"&orderhospprice="+orderprice
	        	+"&orderstime="+ChooseDocTime+"&orderhosptpricetype="+traffictype+"&orderhosptprice="+trafficprice+"&orderhospapricetype="+accommondationtype
	        	+"&orderhospaprice="+accommondationprice+"&orderhospepricetype="+diettype+"&orderhospeprice="+dietprice+"&orderabs="+orderabs;
        //String s=HttpRequest.sendGet("http://192.168.2.2:8080/internetmedical/hospital/PreOrderRequest", Param);
		System.out.println("输出Param参数集："+Param);
		Map<String, Object> doctorlist = new HashMap<>();
		if(docloginid!=null && hosploginid!=null && orderprice!=null) {
			try {
				JSONObject json = HttpRequest.sendPost("PreOrderRequest",Param);
				System.out.println("预约医生返回状态码："+json);
		        Integer code = Integer.parseInt(json.get("code").toString());
		        String msg = json.get("msg").toString();
		        doctorlist.put("code", code);
		        doctorlist.put("msg", msg);
			} catch (Exception e) {
				// TODO: handle exception
				doctorlist.put("code", -200);
		        doctorlist.put("msg", "消息发送失败！");
			}
			
		}else {
			doctorlist.put("code", -100);
	        doctorlist.put("msg", "检查提交数据，不可为空！");
		}
        
        //JSONObject json = HttpRequest.sendPost("http://192.168.2.2:8080/internetmedical/hospital/testjson","name=ltt&sex=male");
        //System.out.println("输出返回的json信息："+json);
        //List<Map<String, String>> list = new ArrayList<>();
        //JSONArray jsonarray = json.getJSONArray("data");
        //System.out.println("jsonarray:"+jsonarray);
        /*for(int i=0;i<jsonarray.length();i++){
        	Map<String,String> map1  = new HashMap<>();
        	JSONObject job = jsonarray.getJSONObject(i);
        	map1.put("name", job.get("name").toString());
        	map1.put("sex", job.get("sex").toString());
        	list.add(map1);
        	System.out.println("数据："+job) ;
        }*/
        //list = DataAnalyze.JsonToList(jsonarray);
        //System.out.println("........"+list);
        //发送 POST 请求
        //String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
        //System.out.println(sr);
        return doctorlist;
	}
	@RequestMapping(value="listtest")
	public ModelAndView list()throws Exception{
		ModelAndView model = new ModelAndView();
		JSONObject json = HttpRequest.sendPost("testjson","name=ltt&sex=male");
        System.out.println("输出返回的json信息："+json);
        //List<String> data = HttpRequest.HttpDoctorList("http://192.168.2.2:8080/internetmedical/hospital/testjson","name=ltt&sex=male");
        List<Map<String, String>> list = new ArrayList<>();
        JSONArray jsonarray = json.getJSONArray("data");
        System.out.println("jsonarray:"+jsonarray);
        /*for(int i=0;i<jsonarray.length();i++){
        	Map<String,String> map1  = new HashMap<>();
        	JSONObject job = jsonarray.getJSONObject(i);
        	map1.put("id", job.get("hospdeptid").toString());
        	map1.put("name", job.get("hospdeptname").toString());
        	list.add(map1);
        	System.out.println(job) ;
        }*/
        list = DataAnalyze.JsonToList(jsonarray);
        System.out.println("........"+list);
        model.addObject("list", list);
        model.setViewName("MainFrame/Doctor/ListTest");
		return model;
	}
	/**
	 * 向表hosporder插入一条记录，生成一条订单记录
	 * @param hosporderCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inserthosporder")
	public @ResponseBody int inserthosporder(@RequestBody HosporderCustom hosporderCustom)throws Exception {
		System.out.println("进入insert");
		System.out.println("输出查询到的数据是："+hosporderCustom.getDoctorid());
		Date date=new Date();
		hosporderCustom.setOrderstate(1);//订单初始状态为等待医生确认
		hosporderCustom.setOrderptime(date);//订单生成时间
		try {
			int result=doctorinfoService.InsertHospOrder(hosporderCustom);
			return result;
		} catch (Exception e) {
			return -1;
		}
	}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/publishillness")
	public ModelAndView publishillness()throws Exception {
		//System.out.println("输出返回的数据是这样的："+hosplevel+"///"+doctortitle+"///"+details);
		//初始化医院等级下拉框
		//List<HosplevelCustom> hosplevelList = hospinfoService.InithosplevelList(null);
		//初始化医生职称下拉框
		//List<DoctortitleCustom> doctortitleList = hospinfoService.InitdoctortitleList(null);
		
		ModelAndView model = new ModelAndView();
		//model.addObject("hosplevel", hosplevel);
		//model.addObject("doctortitle", doctortitle);
		//model.addObject("details", details);
		//model.addObject("hosplevelList", hosplevelList);
		//model.addObject("doctortitleList", doctortitleList);
		model.setViewName("MainFrame/Doctor/PublishIllness");
		return model;
	}
	/**
	 * 获取省,市，区列表
	 * @param city
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getProvince")
	public @ResponseBody Map<String,Object> getProvince(HttpServletRequest request) throws Exception{
		String cityparentcode=request.getParameter("cityparentcode");
		String param = "cityparentcode="+cityparentcode;
		System.out.println("输出参数串param："+param);
		Map<String,Object> Provincemap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject provincejson = HttpRequest.sendPost("listcity", param);
			String jsoncode = provincejson.get("code").toString();
			String jsonmsg = provincejson.get("msg").toString();
			
			Provincemap.put("code", jsoncode);
			Provincemap.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					JSONArray data = provincejson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("citycode", job.get("citycode").toString());
						map.put("cityname", job.get("cityname").toString());
						list.add(map);
					}
					Provincemap.put("list", list);
				} catch (Exception e) {
					Provincemap.put("list", "");
					System.out.println("出错信息是："+e.getMessage());
				}
			}else {
				Provincemap.put("list", "");
			}
		} catch (Exception e) {
			Provincemap.put("code", "-200");
			Provincemap.put("msg", "数据传输出错了！");
			Provincemap.put("list", "");
		}
		//List<City> provinceList = doctorinfoService.getCity(city);
		return Provincemap;
	}
	/**
	 * 初始化医院部门下拉框
	 * @param hospitaldept
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getHospitalDept")
	public @ResponseBody Map<String,Object> getHospitalDept(HttpServletRequest request)throws Exception{
		//List<Hospitaldept> hospitaldeptList = doctorinfoService.getHospitalDept(hospitaldept);
		Integer hospdeptfatherid =Integer.parseInt(request.getParameter("hospdeptfatherid"));
		String param = "hospdeptfatherid="+hospdeptfatherid;
		System.out.println("输出参数串param："+param);
		Map<String,Object> Deptmap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject deptjson = HttpRequest.sendPost("listdept", param);
			String jsoncode = deptjson.get("code").toString();
			String jsonmsg = deptjson.get("msg").toString();
			
			Deptmap.put("code", jsoncode);
			Deptmap.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					JSONArray data = deptjson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hospdeptid", job.get("hospdeptid").toString());
						map.put("hospdeptname", job.get("hospdeptname").toString());
						list.add(map);
					}
					Deptmap.put("list", list);
				} catch (Exception e) {
					Deptmap.put("list", "");
					System.out.println("出错信息是："+e.getMessage());
				}
			}else {
				Deptmap.put("list", "");
			}
		} catch (Exception e) {
			Deptmap.put("code", "-200");
			Deptmap.put("msg", "数据传输出错了！");
			Deptmap.put("list", "");
		}
		return Deptmap;
	}
	/**
	 * 获取医院等级下拉框
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getHospitalLevel")
	public @ResponseBody Map<String,Object> getHospitalLevel()throws Exception{
		Map<String,Object> HospitalLevelmap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject leveljson = HttpRequest.sendPost("listhosplevel", "");
			String jsoncode = leveljson.get("code").toString();
			String jsonmsg = leveljson.get("msg").toString();
			
			HospitalLevelmap.put("code", jsoncode);
			HospitalLevelmap.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					JSONArray data = leveljson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hosplevelid", job.get("hosplevelid").toString());
						map.put("hosplevelname", job.get("hosplevelname").toString());
						list.add(map);
					}
					HospitalLevelmap.put("list", list);
				} catch (Exception e) {
					HospitalLevelmap.put("list", "");
					System.out.println("出错信息是："+e.getMessage());
				}
			}else {
				HospitalLevelmap.put("list", "");
			}
		} catch (Exception e) {
			HospitalLevelmap.put("code", "-200");
			HospitalLevelmap.put("msg", "数据传输出错了！");
			HospitalLevelmap.put("list", "");
		}
		return HospitalLevelmap;
	}
	/**
	 * 获取医生职称下拉框
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getDoctorTitle")
	public @ResponseBody Map<String,Object> getDoctorTitle()throws Exception{
		Map<String,Object> DoctorTitlemap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject titlejson = HttpRequest.sendPost("listdoctortitle", "");
			String jsoncode = titlejson.get("code").toString();
			String jsonmsg = titlejson.get("msg").toString();
			
			DoctorTitlemap.put("code", jsoncode);
			DoctorTitlemap.put("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				try {
					JSONArray data = titlejson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("doctitleid", job.get("doctitleid").toString());
						map.put("doctitlename", job.get("doctitlename").toString());
						list.add(map);
					}
					DoctorTitlemap.put("list", list);
				} catch (Exception e) {
					DoctorTitlemap.put("list", "");
					System.out.println("出错信息是："+e.getMessage());
				}
			}else {
				DoctorTitlemap.put("list", "");
			}
		} catch (Exception e) {
			DoctorTitlemap.put("code", "-200");
			DoctorTitlemap.put("msg", "数据传输出错了！");
			DoctorTitlemap.put("list", "");
		}
		return DoctorTitlemap;
	}
}
